package com.wangdong.ceshirenlianhangxin;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.Window;

import com.wangdong.ceshirenlianhangxin.fragment.BottomButtonFragment;
import com.wangdong.ceshirenlianhangxin.fragment.ResultFragment;
import com.wangdong.ceshirenlianhangxin.fragment.StartPageFragment;

public class MainActivity extends AppCompatActivity {

    public static final String EXTRA_RESULT_CODE = "extra_result_code";

    public static final int STATE_START = 0;
    public static final int STATE_RESULT = 1;

    private StartPageFragment mStartPageFragment = null;
    private BottomButtonFragment mBottomButtonFragment = null;
    private ResultFragment mResultFragment = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        getWindow().requestFeature(Window.FEATURE_NO_TITLE);

        setContentView(R.layout.activity_main);

        switchFragment(STATE_START, getIntent());
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        switch (resultCode) {
            case RESULT_OK:
                switchFragment(STATE_RESULT, intent);
                break;

            default:
                if (intent == null) {
                    intent = new Intent();
                }
                intent.putExtra(EXTRA_RESULT_CODE, resultCode);
                switchFragment(STATE_START, intent);
                break;
        }
    }

    private void switchFragment(int state, Intent intent) {
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        switch (state) {
            case STATE_START:
                if (mStartPageFragment == null) {
                    mStartPageFragment = new StartPageFragment();
                }
                setFragmentArguments(mStartPageFragment, intent);
                transaction.replace(R.id.layout_top, mStartPageFragment);
                break;

            case STATE_RESULT:
                if (mResultFragment == null) {
                    mResultFragment = new ResultFragment();
                }
                setFragmentArguments(mResultFragment, intent);
                transaction.replace(R.id.layout_top, mResultFragment);
                break;
            default:
                break;
        }
        if (mBottomButtonFragment == null) {
            mBottomButtonFragment = new BottomButtonFragment();
        }
        if (intent == null) {
            intent = new Intent();
        }
        intent.putExtra(BottomButtonFragment.EXTRA_STATE, state);
        setFragmentArguments(mBottomButtonFragment, intent);
        transaction.replace(R.id.layout_bottom, mBottomButtonFragment);
        transaction.commitAllowingStateLoss();
    }

    private void setFragmentArguments(Fragment fragment, Intent intent) {
        if (intent == null) {
            return;
        }
        Bundle extras = intent.getExtras();
        if (extras == null) {
            return;
        }
        if (fragment == null) {
            return;
        }
        if (fragment.getArguments() == null) {
            return;
        }
        fragment.getArguments().putAll(extras);
    }
}
